package rmhc
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	
	[SWF(width='807', height='593', backgroundColor='#FFFFFF', frameRate='30')]
	public class FbPreloader extends MovieClip implements IAppPreloader
	{
		public var lod:Loader;
		
		public function FbPreloader()
		{
			super();
			var swf:String;
			var mes:String = stage.loaderInfo.parameters.message;
			//var stgW:Number , stgH:Number;
			//if ( ref && ref.match( "wall" ) != null )
			if ( mes && mes.length == 32 )
			{
				//открываем стену
				swf = "RMHCWall";
			}
			else
			{
				swf = "RMHCApp";
			}
			trace( "load swf" , swf );
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			var LC:LoaderContext = new LoaderContext( true , ApplicationDomain.currentDomain , SecurityDomain.currentDomain );
			lod = new Loader();
			lod.contentLoaderInfo.addEventListener(Event.COMPLETE , onLoadDone );
			//lod.load( new URLRequest( getServerUrl() + swf + ".swf?" + Math.random() ) , LC );
			lod.load( new URLRequest( getServerUrl() + swf + ".swf" ) , LC );
			
			addEventListener( Event.ADDED_TO_STAGE , init );
		}
		protected function init( e:Event ):void
		{
			
		}
		
		
		protected function onLoadDone( e:Event ):void
		{
			//stage.addChild( lod.content );
			trace( "fb load done" , lod.contentLoaderInfo.url );
			addChild( lod.content );
			//this.parent.removeChild( this );
		}
		
		public function getPhpUrl():String
		{
			return getServerUrl();
		}
		
		public function getServerUrl():String
		{
			//if ( stage.loaderInfo.parameters.viewer_id == "34722749" )
			//{
				//return "http://rmhc/flash/";
			/*}
			else
			{
				return "http://mind2create.ru/vk/rmhc/";
			}*/
			//return "http://rmhc/flash/";
			return "http://mind2create.ru/rmhc/";
		}
		
		public function getAppUrl():String
		{
			return "http://apps.facebook.com/mchappyday_ru/";
		}
		public function navigateToApp():void
		{
			navigateToURL( new URLRequest( getAppUrl() ) , "_top" );
		}
		public function getPostId():String
		{
			return stage.loaderInfo.parameters.message;
		}
	}
}